const app = getApp()
const api = require('../../../api/index.js')
const util = require("../../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: [{
        url: '/pages/img/1.png'
      },
      {
        url: '/pages/img/2.png'
      },
      {
        url: '/pages/img/3.png'
      }
    ],
    autoplay: true,
    //
    interval: 3000,
    duration: 1000,
    tabData: [{
        name: "个人提升",
        active: true,
        checked: 'true',
        phone: '',
        button: false
      },
      {
        name: "亲子教学",
        active: false,
        phone: '',
        button: false
      },
      {
        name: "团队聚会",
        active: false,
        phone: '',
        button: false
      }
    ],
    indexFormOne: {},
    indexFormTwo: {},
    indexFormThree: {},
    indexsData: {
      
    }, // 首页请求数据
    btnActive: [false, false, false],
    address: {
      region_name: '北京市'
    }, //当前位置
    imgSrc: "",
    hideView: false,
    phoneIndex:'',
    pay:'',
    tokenState:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
    let invite_id = options.invite_id;
    console.log(options, '测试分享数据')
    if (invite_id) {
      wx.setStorageSync('invite_id', invite_id)
    }
    console.log(invite_id);
    this.setData({
      imgSrc: api.imgPathUrl  
    })
    app.locationCity()
    this.idnesData()
    wx.showLoading({
      title: '加载中',
    })
  },
  getPhoneNumber: function (e) {
    let sessionKeyS = wx.getStorageSync('session_key').session_key;
    console.log(sessionKeyS);
    if (sessionKeyS == '' || sessionKeyS == undefined) {
      app.Logins();
    }
    let invite_id = wx.getStorageSync('invite_id');
    let data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      sessionKey: sessionKeyS,
      invite_id: invite_id
    }
    console.log(data);
    api.getPhoneNumber(data, (res) => {
      if (res.data.code == 50003) {
        this.setData({
          tokenState: true
        })
        wx.showToast({
          title: '亲，登录需要授权哦~',
          icon: 'none',
          duration: 2000
        })
      } else {
        this.setData({
          userData: res.data.result,
          tokenState: false
        })
        
        app.globalData.Refresh = true;
        wx.setStorageSync('token', {
          "token": res.data.result.token,
          "user_mobile": res.data.result.user_mobile
        });
        // wx.navigateTo({
        //   url: `/pages/my/Share/Share`,
        // })
        console.log(wx.getStorageSync('token'))
      }
    })
  },
  classify:function(e){
      console.log(e)
    let id = e.currentTarget.dataset.category

    if (id == 1555){
      wx.navigateTo({
        url: `/pages/indexs/classify/classify`,
      }) 
    }else{
      wx.navigateTo({
        url: `/pages/indexs/channel/channel?id=${id}`,
      })
    }
  },
  idnesData:function(){
    console.log(wx.getStorageSync('key').longitude)
    let data = {
      longitude: wx.getStorageSync('key').longitude || 116.44355,
      latitude: wx.getStorageSync('key').latitude || 39.9219
    }
    api.indexS(data,(res)=>{
      console.log(res)
      this.setData({
        indexsData:res.data.result
      })
      this.timeout()
      this.setData({
        pay: res.data.result.activeorder[0]
      })
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  onItemClick: function (e) {
    
    let that = this
    if("123" === e.currentTarget.dataset.url){
    
      if (0 === getApp().messages()) {
        that.setData({
          tokenState: true
        })
      }else{
        wx.navigateTo({
          url: `/pages/vipCard/index`,
        })
      }
      return false;
    }

    let name = e.currentTarget.dataset.name;
    let id = e.currentTarget.dataset.postid;
    
    let urll = e.currentTarget.dataset.url;
    console.log(e, id)
    
    // wx.navigateTo({
    //   url: "/pages/activity/activity/activity?url=" + urll
    // })
    // console.log(e.currentTarget.dataset,id,name)
    let tokenState = getApp().messages();
    if (id == undefined){
      if (tokenState === 0) {
        that.setData({
          tokenState: true
        })
        
      } else {
        wx.navigateTo({
          url: `/pages/my/Share/Share`,
        })
      }
    }
    App.zhuge.track('记录首页baner-点击调取活动名称', {
      '活动名称': name,
      '活动ID': id
    });

  },
  radioChange: function (e) {
    let index = e.currentTarget.dataset.index;
    this.data.tabData.forEach((item, index) => {
      let key = `tabData[${index}].active`;
      this.setData({
        [key]: false
      })
    })
    this.setData({
      [`tabData[${index}].active`]: true
    })
    // console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  phoneSYz: function (obj, index) {
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(obj)) {
      this.setData({
        [`btnActive[${index}]`]: false
      })
      console.log(index);
      return false;
    } else {
      this.setData({
        [`btnActive[${index}]`]: true
      })
      return true;
    }
  },
  phones: function (e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      [`tabData[${index}].phone`]: e.detail.value,
      phoneIndex:index
    })
    console.log(index)
    let phon = e.detail.value;
    this.phoneSYz(phon, index);
  },
  toPlain: function (e) {
    console.log(e)
    let index = e.target.dataset.index;
    let phone = this.data.tabData[index].phone;
    let typeState = e.target.dataset.type;
    let num = Number(index) + 1;
    let data = {
      type: num,
      mobile: phone
    }
    if (typeState) {
      this.indexForm(data)
    }
  },
  indexForm: function (data) {
    api.indexForm(data, (res) => {
      console.log(res);
      if (res.data.code == 200) {
        this.setData({
          hideView: true
        })
      }
    })
  },
  indexOk: function () {
    console.log(this.data.phoneIndex)
    let phoneIndex = this.data.phoneIndex
    let tabDataPhon = `tabData[${phoneIndex}].phone`
    
    this.setData({
      hideView: false,
      [tabDataPhon]:'',
      [`btnActive[${phoneIndex}]`]: false
    })
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  select:function(e){ // 9块9选择等
    console.log(e)
    let index = e.currentTarget.dataset.in
    if(index === 0){
      wx.navigateTo({
        url: '/pages/indexs/index9.9/index9.9',
      })
    }
    if(index === 2){
      wx.navigateTo({
        url: '/pages/indexs/sportRim/sportRim',
      })
    }
    if(index === 3){
      wx.navigateTo({
        url: '/pages/lookVennue/lookVennue/lookVennue',
      })
    }
    if (index === 1) {
      wx.navigateTo({
        url: '/pages/indexs/dingshou/dingshou',
      })
    }
  },
  lbc_buy: function (e) { // 限时特惠立即抢购
    console.log(e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/lookVennue/course/course?id=${id}`,
    })
  },
  lbc_buyAll:function(){ //限时特惠 查看全部
      wx.navigateTo({
        url: '/pages/indexs/Leisure/Leisure',
      })
  },
  qualityAll:function(e){ //素质养成 查看全部

    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/indexs/quality/quality?id=${id}`,
    })
  },
  timeout:function(){
    let activeorder = this.data.indexsData.activeorder
    let num = 0  
    let that = this
    setInterval(function(){
        num ++
      if (num == activeorder.length){
          num = 0
        }
      that.setData({
        pay: that.data.indexsData.activeorder[num]
      })
      // console.log(num, that.data.pay)
    },60000)

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  phoneCall: function (e) { // 底部打电话
    let phone;
    let data = e.currentTarget.dataset.phone;
    App.zhuge.track(' 合作咨询电话', {});
    util.phone(data, this.data.indexsData.cooperation.mobile);

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})